package com.java.frontweb.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RedisConfiguration
 * @Description jedis 配置
 * @Author Trigger Wu
 * @Date 2018/9/1710:05
 * @Version 1.0
 **/
@Configuration
public class RedisConfiguration {
    @Bean
    public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
                                            @Value("${jedis.pool.config.maxIdle}")int maxIdle,
                                            @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMaxWaitMillis(maxWaitMillis);
        return config;
    }

    @Bean
    public JedisShardInfo jedisShardInfo(@Value("${jedis.pool.host}")String host,
                                         @Value("${jedis.pool.port}")int port){
        JedisShardInfo shardInfo = new JedisShardInfo(host, port);
        return shardInfo;
    }

    @Bean
    public ShardedJedisPool jedisPool(JedisPoolConfig config, JedisShardInfo shardInfo){
        List<JedisShardInfo> shardInfos = new ArrayList<>();
        shardInfos.add(shardInfo);
        ShardedJedisPool shardedJedisPool = new ShardedJedisPool(config, shardInfos);
        return shardedJedisPool;
    }
}
